//
//  MyViewController.m
//  HelloWorld
//
//  Created by Cara Landreth on 11-04-18.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MyViewController.h"


@implementation MyViewController
@synthesize textField;
@synthesize label;
@synthesize anotherLabel;
@synthesize userName;
@synthesize showing;

@synthesize button;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [label release];
    [anotherLabel release];
    [textField release];
    [userName release];
    [button release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    CGRect frame = CGRectMake(20.0, 68.0, 280.0, 31.0);
    UITextField * aTextField = [[UITextField alloc] initWithFrame:frame];
    self.textField = aTextField;
    [aTextField release];
    
    textField.textAlignment = UITextAlignmentCenter;
    textField.borderStyle = UITextBorderStyleRoundedRect;
    
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.returnKeyType = UIReturnKeyDone;
    textField.delegate = self;
    
    textField.placeholder = @"Your name";
    
    [self.view addSubview:textField];
    
    frame = CGRectMake(20.0, 175.0, 280.0, 31.0);
    UILabel * aLabel = [[UILabel alloc] initWithFrame:frame];
    self.label = aLabel;
    [aLabel release];
    
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"Hello __________";
    
    [self.view addSubview:label];
    
    frame = CGRectMake(20.0, 220.0, 280.0, 31.0);
    UILabel * aNotherLabel = [[UILabel alloc] initWithFrame:frame];
    self.anotherLabel = aNotherLabel;
    [anotherLabel  release];
    
    anotherLabel.textAlignment = UITextAlignmentCenter;
    anotherLabel.text = @"Testing";
    
    frame = CGRectMake(125.0, 400.0, 70.0, 31.0);
    UIButton * aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.button = aButton;
    
    button.frame = frame;
    [button setTitle:@"Hello" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(greetingPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    showing = FALSE;
}

- (void)viewDidUnload
{
    [self setLabel:nil];
    [self setTextField:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if(theTextField == textField) {
        [textField resignFirstResponder];
    }
    return YES;
}

- (IBAction)greetingPressed:(id)sender {
    self.userName  = textField.text;
    
    NSString *nameString = self.userName;
    if([nameString length] == 0){
        nameString = @"No name";
    }
    
    NSString *greetingString = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
    label.text = greetingString;
    [greetingString release];
    
    if(!showing){
        [self.view addSubview:anotherLabel];
        showing = TRUE;
    }
    else{
        [anotherLabel removeFromSuperview];
        showing = FALSE;
    }
}
@end
